package com.a918.service.impl;

import com.a918.dao.*;
import com.a918.dto.BwgData;
import com.a918.entity.FrontFcCheck;
import com.a918.entity.FrontMember;
import com.a918.entity.FrontUnion;
import com.a918.entity.MemBwgWithBLOBs;
import com.a918.service.BackShowService;
import com.a918.util.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class BackShowServiceImp implements BackShowService {
    private FrontMemberMapper frontMemberMapper;
    private FrontUnionMapper frontUnionMapper;
    private MemBwgMapper memBwgMapper;
    private FrontFcCheckMapper frontFcCheckMapper;
    private UserMapper userMapper;

    @Autowired
    public BackShowServiceImp(FrontUnionMapper frontUnionMapper, FrontMemberMapper frontMemberMapper,MemBwgMapper memBwgMapper, FrontFcCheckMapper frontFcCheckMapper,UserMapper userMapper){
        this.frontMemberMapper=frontMemberMapper;
        this.frontUnionMapper=frontUnionMapper;
        this.memBwgMapper=memBwgMapper;
        this.frontFcCheckMapper=frontFcCheckMapper;
        this.userMapper = userMapper;
    }
    @Override
    public FrontUnion getFrontUnionByUserid(Integer userid){
        return frontUnionMapper.getByUserid(userid);
    }

    @Override
    public FrontMember getFrontMemberById(Integer id){
        return frontMemberMapper.selectByPrimaryKey(id);
    }

    @Override
    public void changeFrontUnionDescribe(FrontUnion frontUnion){
        Boolean exist= frontUnionMapper.getByUserid(frontUnion.getUserid()) != null;
        if(exist){
            frontUnion.setId(frontUnionMapper.getByUserid(frontUnion.getUserid()).getId());
            frontUnionMapper.updateByPrimaryKeySelective(frontUnion);
        }else {
            frontUnionMapper.insertSelective(frontUnion);
        }
    }

    @Override
    public void changeFrontMemberDescribe(FrontMember frontMember){
        Boolean exist= frontMemberMapper.getByBwgid(frontMember.getBwgid()) != null;
        if(exist){
            frontMember.setId(frontMemberMapper.getByBwgid(frontMember.getBwgid()).getId());
            frontMemberMapper.updateByPrimaryKeySelective(frontMember);
        }else {
            frontMemberMapper.insertSelective(frontMember);
        }
    }

    @Override
    public void delBwgztById(Integer id){
        frontMemberMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<BwgData> selectBwgList(Map<String, String> paramMap,Integer userid){
        int pno=Integer.parseInt(paramMap.get("page"));
        int pageSize=Integer.parseInt(paramMap.get("limit"));
        PageHelper.startPage(pno, pageSize);
        // 1. 开启分页
        List<FrontMember> UserPoList = frontMemberMapper.selectByUserid(userid);
        // 3. 封装list到PageInfo对象中自动分页
        PageInfo<FrontMember> userPoPageInfo = new PageInfo<>(UserPoList);
        // 4. 转换为UserVo类型的PageInfo对象
        PageInfo<BwgData> userVoPageInfo = PageUtils.PageInfo2PageInfoVo(userPoPageInfo);
        // 5. 创建需要分页的UserVoList
        List<BwgData> userVoList = new ArrayList<>();
        // 6. 遍历UserPo给UserVoList初始化
        for(FrontMember userpo : UserPoList){
            BwgData uservo = new BwgData();
            uservo.setId(userpo.getId());
            switch (userpo.getStatus()) {
                case "dsh":
                    uservo.setStatus("待审核");
                    break;
                case "yfb":
                    uservo.setStatus("审核通过");
                    break;
                case "dxg":
                    uservo.setStatus("已打回");
                    break;
            }
            uservo.setUsername(userMapper.selectByPrimaryKey(userpo.getUserid()).getUsername());
            uservo.setName(userpo.getName());
            uservo.setCreattime(userpo.getCreattime());
            uservo.setProvince(userpo.getProvince());
            userVoList.add(uservo);
        }
        for (BwgData uservo : userVoList) {
            userVoPageInfo.getList().add(uservo);
        }
        return userVoPageInfo;
    }

    @Override
    public List<MemBwgWithBLOBs> getAvalMuseByUserid(Integer userid){
        return memBwgMapper.getAvalMuseByUserid(userid);
    }

    @Override
    public FrontFcCheck selectFrontFcCheckById(Integer id,String type){
        FrontFcCheck frontFcCheck=frontFcCheckMapper.selectByTypeAndCheckid(id,type);
        if(frontFcCheck.getOpinion()==null)
        {frontFcCheck.setOpinion("无");}
        return frontFcCheck;
    }

    @Override
    public MemBwgWithBLOBs getBwgById(Integer id){
         return memBwgMapper.selectByPrimaryKey(id);
    }
}
